package org.eenie.kdassistant.hooker;

import com.alibaba.fastjson.JSON;

import de.robv.android.xposed.IXposedHookLoadPackage;
import de.robv.android.xposed.XC_MethodHook;
import de.robv.android.xposed.XposedBridge;
import de.robv.android.xposed.XposedHelpers;
import de.robv.android.xposed.callbacks.XC_LoadPackage;

/**
 * Created by Eenie on 2017/8/7.
 * Email: eeniework@gmail.com
 * Des:
 */

public class YDHooker implements IXposedHookLoadPackage {

    private String PKG_NAME = "com.yunda.bmapp";


    @Override
    public void handleLoadPackage(XC_LoadPackage.LoadPackageParam param) throws Throwable {


        if (!param.packageName.equals(PKG_NAME)) {
            return;
        }

        XposedBridge.log("hook success pkg = " + param.packageName);

        ClassLoader classLoader = param.classLoader;


        XposedHelpers.findAndHookMethod(classLoader.loadClass("com.yunda.bmapp.common.net.a.a"),
                "security",  classLoader.loadClass("com.yunda.bmapp.common.net.b"),String.class,new XC_MethodHook(){

                    @Override
                    protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
                        XposedBridge.log("security param0 = " + JSON.toJSONString(param.args[0]));
                        XposedBridge.log("security param1 = " + param.args[1].toString());

                    }

                    @Override
                    protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                        XposedBridge.log("security res = " + param.getResult().toString());
                    }
                });



        XposedHelpers.findAndHookMethod(classLoader.loadClass("com.yunda.bmapp.common.f.e"),
                "encrypt", String.class,new XC_MethodHook(){

                    @Override
                    protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
                        XposedBridge.log("encrypt before param0 = " + param.args[0].toString());


                    }

                    @Override
                    protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                        XposedBridge.log("encrypt after param0 = " + param.args[0].toString());
                        XposedBridge.log("encrypt res = " + param.getResult().toString());
                    }
                });


        XposedHelpers.findAndHookMethod(classLoader.loadClass("com.yunda.crypt.NativeEncryption"),
                "JNI_IEncrypt", String.class, classLoader.loadClass("com.yunda.crypt.EDOutputData"), new XC_MethodHook() {

                    @Override
                    protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
                        XposedBridge.log("JNI_IEncrypt before param0 = " + param.args[0].toString());
                        XposedBridge.log("JNI_IEncrypt before param1 = " + JSON.toJSONString(param.args[1]));


                    }

                    @Override
                    protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                        XposedBridge.log("JNI_IEncrypt res = " + param.getResult().toString());
                    }
                });


//        String paramString1, int paramInt, String paramString2, String paramString3, String paramString4
        XposedHelpers.findAndHookMethod(classLoader.loadClass("com.yunda.crypt.NativeEncryption"),
                "JNI_EDCodeInstance", String.class, int.class, String.class, String.class, String.class, new XC_MethodHook() {

                    @Override
                    protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
                        XposedBridge.log("JNI_EDCodeInstance before param0 = " + param.args[0].toString());
                        XposedBridge.log("JNI_EDCodeInstance before param1 = " + param.args[1].toString());
                        XposedBridge.log("JNI_EDCodeInstance before param2 = " + param.args[2].toString());
                        XposedBridge.log("JNI_EDCodeInstance before param3 = " + param.args[3].toString());
                        XposedBridge.log("JNI_EDCodeInstance before param4 = " + param.args[4].toString());
                    }

                    @Override
                    protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                        XposedBridge.log("JNI_EDCodeInstance res = " + param.getResult().toString());
                    }
                });

        XposedHelpers.findAndHookMethod(classLoader.loadClass("com.yunda.crypt.NativeEncryption"),
                "JNI_IDecrypt",  String.class, classLoader.loadClass("com.yunda.crypt.EDOutputData"), new XC_MethodHook() {

                    @Override
                    protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
                        XposedBridge.log("JNI_IDecrypt before param0 = " + param.args[0].toString());
                        XposedBridge.log("JNI_IDecrypt before param1 = " + param.args[1].toString());

                    }

                    @Override
                    protected void afterHookedMethod(MethodHookParam param) throws Throwable {

                        Object object = param.args[1];

                        XposedBridge.log("JNI_EDCodeInstance res = " + object.getClass().getMethod("GetData").invoke(object).toString());
                    }
                });







        XposedHelpers.findAndHookMethod(classLoader.loadClass("com.yunda.bmapp.common.net.a.a"),
                "a", String.class, String.class, new XC_MethodHook() {

                    @Override
                    protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
                        XposedBridge.log("sign param0 = " + param.args[0].toString());
                        XposedBridge.log("sign before param1 = " + param.args[1].toString());



                    }

                    @Override
                    protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                        XposedBridge.log("sign res = " + param.getResult().toString());
                    }
                });

//        XposedHelpers.findAndHookMethod(classLoader.loadClass("com.yunda.bmapp.common.f.e"),
//                "doMD5X32Encrypt", String.class, new XC_MethodHook() {
//
//                    @Override
//                    protected void afterHookedMethod(MethodHookParam param) throws Throwable {
//                        XposedBridge.log("md5 src = " + param.args[0].toString());
//                        XposedBridge.log("md5 res = " + param.getResult().toString());
//                    }
//                });
//
//        XposedHelpers.findAndHookMethod(classLoader.loadClass("com.yunda.bmapp.common.f.e"),
//                "doSHAR1Encrypt", String.class, new XC_MethodHook() {
//
//                    @Override
//                    protected void afterHookedMethod(MethodHookParam param) throws Throwable {
//                        XposedBridge.log("SHAR1 src = " + param.args[0].toString());
//                        XposedBridge.log("SHAR1 res = " + param.getResult().toString());
//                    }
//                });









    }




}
